﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HandBook.Data;
using Telerik.Web.UI;
using System.IO;
using HandBook.Common;

public partial class Admin_UploadImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        switch (rdTypes.SelectedValue.ToString())
        {
            case "Flash" :
                RadUpload1.MaxFileInputsCount = 1;
                break;
            case "Banner" :
                RadUpload1.MaxFileInputsCount = 1;
                break;
            case "Logo" :
                RadUpload1.MaxFileInputsCount = 1;
                break;
            default:
                RadUpload1.MaxFileInputsCount = 5;
                break;
        }
    }
    protected void buttonSubmit_Click(object sender, EventArgs e)
    {
        int ID = Convert.ToInt32(Request.Params["RestID"].ToString());
        var rest = HandBookData.EatDrinkDB.Restaurants.Single(r => r.RestID == ID);
        string folderName = string.Empty;

        if (rest.ImageFolderName == null)
        {
            folderName = "blank";
        }
        else
        {
            folderName = rest.ImageFolderName;
        }

        if (FileManager.CheckDirectoryExisting(Server.MapPath("../Images/Restaurants/" + folderName)))
        {
            switch (rdTypes.SelectedValue.ToString())
            {
                case "Flash" :
                    UploadImage(folderName, "flash.swf");
                    rest.Flash = "~/Images/Restaurants/flash.swf";
                    HandBookData.EatDrinkDB.SubmitChanges();
                    break;
                case "Banner" :
                    UploadImage(folderName, "banner.gif");
                    break;
                case "Logo" :
                    UploadImage(folderName, "logo.gif");
                    break;
                default:
                    UploadImage(folderName, "");
                    break;
            }
            //ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
            //Response.Redirect("UploadImage.aspx?RestID=" + ID.ToString());
            Server.Transfer("UploadImage.aspx?RestID=" + ID.ToString());
        }
        else
        {
            if (folderName != "blank")
            {
                Directory.CreateDirectory(Server.MapPath("../Images/Restaurants/" + rest.ImageFolderName));
                switch (rdTypes.SelectedValue.ToString())
                {
                    case "Flash":
                        UploadImage(folderName, "flash.swf");
                        rest.Flash = "~/Images/Restaurants/flash.swf";
                        HandBookData.EatDrinkDB.SubmitChanges();
                        break;
                    case "Banner":
                        UploadImage(folderName, "banner.gif");
                        break;
                    case "Logo":
                        UploadImage(folderName, "logo.gif");
                        break;
                    default:
                        UploadImage(folderName, "");
                        break;
                }
                //ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
                Server.Transfer("UploadImage.aspx?RestID=" + ID.ToString());
            }
            else
            {
                lbInfo.Text = "Folder hình của nhà hàng này chưa có trong cơ sở dữ liệu.<br/> Vui lòng nhập tên folder của nhà hàng trước khi thực hiện việc upload hình!";
            }
        }
    }

    private void UploadImage(string folderName, string imageName)
    {
        if (RadUpload1.UploadedFiles.Count > 0)
        {
            foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
            {
                string targetFolder = Server.MapPath("../Images/Restaurants/" + folderName);
                if (!imageName.Equals(""))
                {
                    validFile.SaveAs(Path.Combine(targetFolder, validFile.GetName().Replace(validFile.GetName(), imageName)), true);
                }
                else
                {
                    validFile.SaveAs(Path.Combine(targetFolder, validFile.GetName()), true);
                }
            }
            Response.Write("<script>alert('Upload thành công')</script>");
        }
        else
        {
            lbInfo.Text = "Chưa có file nào hoặc định dạng file không chính xác";
        }
    }
}
